﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.MapEditor.Controls
{
    public partial class ToolSelector : UserControl
    {
        public class ToolButton
        {
            public Image ButtonIcon;
            public Tool Tool;
            public Rectangle Rect;
        }

        private EditorState ES;

        public class ToolPickedEventArgs : EventArgs
        {
        }

        public delegate void ToolPickedEventHandler(Object sender, ToolPickedEventArgs e);
        public event ToolPickedEventHandler ToolPicked;

        public void SetEditorState(EditorState ES)
        {
            int XPlace = 0;
            int YPlace = 0;

            foreach (System.Reflection.Assembly Assembly in ES._config._loadedAssemblies)
            {
                foreach (System.Type PossibleTool in Assembly.GetTypes())
                {
                    if (PossibleTool.IsSubclassOf(typeof(Tool)))
                    {
                        bool HasAttribute = false;
                        foreach (var Attribute in PossibleTool.GetCustomAttributes(false))
                            if (Attribute.GetType() == typeof(Tools.SelectableToolAttribute)) HasAttribute = true;
                        if (!HasAttribute) continue;
                        var Button = new ToolButton();
                        Button.Tool = Activator.CreateInstance(PossibleTool) as Tool;
                        Button.ButtonIcon = Button.Tool.GetIcon();
                        Button.Rect = new Rectangle(XPlace, YPlace, ButtonWidth, ButtonHeight);
                        XPlace += ButtonWidth + 1;
                        if (XPlace + ButtonWidth + 1 > Width)
                        {
                            XPlace = 0;
                            YPlace += ButtonHeight + 1;
                        }
                        Buttons.Add(Button);

                        ES.OnConsoleOutput("New tool : " + PossibleTool.Name + " from " + Assembly.GetName().Name + "\r\n");
                    }
                }
            }

            this.ES = ES;
        }

        public List<ToolButton> Buttons = new List<ToolButton>();
        public int ButtonWidth { get; set; }
        public int ButtonHeight { get; set; }

        public ToolSelector()
        {
            ButtonWidth = 64;
            ButtonHeight = 64;
            InitializeComponent();
            BGBrush = new SolidBrush(this.BackColor);
            ItemBorderBrush = new Pen(Color.Black);
            SelectedItemBrush = new Pen(Color.Red);

        }

        Brush BGBrush;
        Pen ItemBorderBrush;
        Pen SelectedItemBrush;

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(BGBrush, 0, 0, Width, Height);
            foreach (var Button in Buttons)
            {
                e.Graphics.DrawImage(Button.ButtonIcon, Button.Rect);
                e.Graphics.DrawRectangle(
                    Button.Tool == ES.SelectedTool ? SelectedItemBrush : ItemBorderBrush, Button.Rect);
            }

            e.Graphics.Flush();
        }

        private void ToolSelector_Click(object sender, EventArgs e)
        {
            var MouseArgs = e as MouseEventArgs;
            //var MousePoint = this.PointToClient(MouseArgs.Location);
            foreach (var Button in Buttons)
            {
                if (Button.Rect.Contains(MouseArgs.Location))
                {
                    ES.SelectedTool = Button.Tool;
                    if (ToolPicked != null) ToolPicked(this, new ToolPickedEventArgs());
                }
            }
            this.Invalidate();
        }
    }
}
